package com.lvhr.rpc.proxy;

import com.lvhr.rpc.model.RpcRequest;
import com.lvhr.rpc.utils.SignatureUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.HmacUtils;


/**
 * 专为动态代理设计的签名器
 */
@Slf4j
public class ProxySigner {
    private final String secretKey;

    public ProxySigner(String secretKey) {
        this.secretKey = secretKey;
    }

    public void sign(RpcRequest request) throws Exception {
        String signContent = SignatureUtils.buildSignContent(request);
        log.debug("proxySinger开始签名校验 : {}",SignatureUtils.hmacSha256("salt",signContent));
        // 调试日志（与验证器保持一致）
//        System.out.println("signContent签名内容: " + signContent);
//        System.out.println("key签名内容: " + secretKey);

        // 计算签名
        String signature = SignatureUtils.hmacSha256(secretKey,signContent);
        request.getAttachments().put("X-Signature", signature);
//        System.out.println("signature签名内容: " + signature);
    }

}
